﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Economo.Services.BusinessLogic;
using Economo.Services.BusinessEntities;

namespace DataLayerTestProject
{
    [TestClass]
    public class TestCreditCards
    {

        [TestMethod]
        public void TC_CreditCards_01_GetCreditCards()
        {
            CreditCardsBusinessLogic bl = new CreditCardsBusinessLogic();
            List<CreditCard> cards = bl.GetCreditCards();
            Assert.AreEqual(cards.Count, 3);
            List<CreditCard> citiCards = bl.GetCreditCards(2);
            Assert.AreEqual(citiCards.Count, 2);
            CreditCard santanderVisa = bl.GetCardById(1,2);
            Assert.IsNotNull(santanderVisa);
            CreditCard citiVisa = bl.GetCardById(3);
            Assert.IsNotNull(citiVisa);
            List<CreditCard> bankNada = bl.GetCreditCards(100);
            Assert.AreEqual(bankNada.Count, 0);
            CreditCard nada = bl.GetCardById(100);
            Assert.IsNull(nada);
        }
        [TestMethod]
        public void TC_CreditCards_02_InsertAndDeleteCreditCards()
        {
            CreditCardsBusinessLogic bl = new CreditCardsBusinessLogic();
            Int32 preInsert = bl.GetCreditCards().Count;
            CreditCard newCard = new CreditCard();
            newCard.BankId = 1;
            newCard.Description = "borrar";
            newCard.Expiration = null;
            byte newId = bl.Insert(newCard);
            Int32 postInsert = bl.GetCreditCards().Count;
            Assert.AreEqual(preInsert + 1, postInsert);
            bl.Delete(newCard.BankId, newId);
            Int32 postDelete = bl.GetCreditCards().Count;
            Assert.AreEqual(postInsert - 1, postDelete);
            try
            {
                bl.Delete(1, 150);
                Assert.Fail("Wrong Delete didn`t throw an error message");
            }
            catch
            {
                //This delete must fail
            }
        }

        [TestMethod]
        public void TC_CreditCards_03_UpdateCreditCards()
        {
            CreditCardsBusinessLogic bl = new CreditCardsBusinessLogic();
            CreditCard oldCard = bl.GetCardById(2);
            CreditCard toUpdate = new CreditCard();
            toUpdate.Id = 2;
            toUpdate.BankId = oldCard.BankId;
            toUpdate.Description = "Testing Description";
            bl.Update(toUpdate);
            CreditCard updatedCard = bl.GetCardById(2);
            Assert.AreEqual(updatedCard.Description, "Testing Description");
            bl.Update(oldCard);
        }
    }    
}
